package com.running.crm.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.running.crm.CrmConstants.Staff;
import com.running.dao.GenericDao;
import com.running.datagrid.DataGridProperty;
import com.running.util.RunningBeanUtils;
import com.running.util.StringUtils;
import com.running.util.WebUtils;
public abstract class AbstractController implements Controller{
	private Map<String,String> mapView;
	protected String successView,failView,ajaxFailView,viewPage,dir;
	protected String maxAuthScope = "";
	protected String requestUri;
	protected String method;
	
	protected String MSG_SUCCESS = "操作执行成功!";//操作执行成功
	protected String MSG_FAIL = "操作执行失败!";//操作执行异常失败
	protected String MSG_NO_PRIVILEGE = "您没有权限执行该操作!";
	protected String MSG_NO_DATA_FOUND = "没有找到数据!";
	protected String MSG_DATA_NO_UNIQUE = "数据重复!";
	protected long start = 0;
	protected Log logger = LogFactory.getLog(this.getClass());
	
	public final ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response){
		WebUtils.addHeaders(response);
		requestUri = WebUtils.getFileName(request.getRequestURI());
		method = StringUtils.varFormat(request.getParameter("method"));
		String url;
		if("".equals(method)){			
			url = requestUri;
		}else{
			url = method+StringUtils.capitalize(requestUri);
		}
		viewPage = this.mapView.get(url);
		
//		判断是否是直接跳转请求
		if("".equals(StringUtils.varFormat(viewPage))){
			try {
				viewPage = dir+url;
				Object[] parameters = {request,response};
				long start = (new Date()).getTime();
				ModelAndView returnModelAndView = (ModelAndView)MethodUtils.invokeMethod(this, method,parameters);
				long end = (new Date()).getTime();
logger.info("系统运行提示:bean=["+this.requestUri+"],method=["+method+"],Timespan=["+String.valueOf(end-start)+" ms]");
				return returnModelAndView;
			} catch (Exception e) {
				e.printStackTrace();
				request.setAttribute("message", this.MSG_FAIL);
				return new ModelAndView(failView);
			}
		}else{
			return new ModelAndView(viewPage);
		}
	}
		
	public void copyProperties(Object to, Object from) {
    		RunningBeanUtils.copyProperties(to, from);
    }
	
	public void copyProperties(Object bean,Map from){
		RunningBeanUtils.populate(bean, from);
	}

	public Map getMapView() {
		return mapView;
	}
	public void setMapView(Map mapView) {
		this.mapView = mapView;
	}
	
	public String getSuccessView() {
		return this.successView;
	}
	public void setSuccessView(String successView) {
		this.successView = successView;
	}
	
	public String getFailView() {
		return this.failView;
	}
	public void setFailView(String failView){
		this.failView = failView;
	}
	
	public void setDir(String dir) {
		this.dir = dir;
	}
}